package com.itheima.dao;

import com.itheima.pojo.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * 预约 持久层接口
 */
public interface OrderDao {

    /**
     * 根据条件查询预约情况
     *
     * @param order 预约
     * @return 预约集合
     */
    @Select("<script> select * from t_order <where>" +
            "<if test=\"id!=null\">and id = #{id} </if> " +
            "<if test=\"memberId!=null\">and member_id = #{memberId} </if>  " +
            "<if test=\"orderDate!=null\">and orderDate = #{orderDate} </if> " +
            "<if test=\"orderType!=null\">and orderType = #{orderType} </if>" +
            "<if test=\"orderStatus!=null\">and orderStatus = #{orderStatus} </if> " +
            "<if test=\"setmealId!=null\">and setmeal_id = #{setmealId} </if> </where></script>")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "memberId", column = "member_id"),
            @Result(property = "setmealId", column = "setmeal_id")
    })
    List<Order> findByConditon(Order order);

    /**
     * 添加预约  查询添加的主键
     *
     * @param order 预约
     */
    @Insert("insert into t_order values(null,#{memberId},#{orderDate},#{orderType},#{orderStatus},#{setmealId}) ")
    @SelectKey(statement = "select last_insert_id()", keyColumn = "id", keyProperty = "id", before = false, resultType = Integer.class)
    void add(Order order);

    /**
     * 根据id查询会员名称 套餐名称 预约日期  预约类型
     *
     * @param id 预约id
     * @return 会员名称 套餐名称 预约日期  预约类型
     */
    @Select("select m.name member,s.name setmeal,o.orderDate orderDate,o.orderType orderType from t_order o ,t_member m,t_setmeal s where o.member_id=m.id and o.setmeal_id=s.id and o.id=#{id}")
    Map findById(Integer id);

    /**
     * 根据本日日期查询已预约的数量
     *
     * @param today 本日日期
     * @return 已预约的数量
     */
    @Select("select reservations from t_ordersetting where orderDate=#{today}")
    Integer findOrderCountByDate(String today);

    /**
     * 查询本周的预约人数
     *
     * @param thisWeekMonday 本周日期
     * @return 预约人数
     */
    @Select("select count(1) from t_ordersetting where orderDate>=#{thisWeekMonday} ")
    Integer findOrderCountByThisWeek(String thisWeekMonday);

    /**
     * 查询本月的预约人数
     *
     * @param firstDay4ThisMonth 本月日期
     * @return 预约人数
     */
    @Select("select count(1) from  t_ordersetting where orderDate>=#{firstDay4ThisMonth}")
    Integer findOrderCountAndByThisMonth(String firstDay4ThisMonth);

    /**
     * 查询本日 应约人数
     *
     * @param today 本日日期
     * @return 应约人数
     */
    @Select("select count(1) from t_order where orderDate=#{today} and orderStatus ='已到诊' ")
    Integer findVisitsCountByDate(String today);

    /**
     * 查询本周应约人数
     *
     * @param thisWeekMonday 本周日期
     * @return 应约人数
     */
    @Select("select count(1) from t_order where orderDate>=#{thisWeekMonday} and orderStatus ='已到诊' ")
    Integer findVisitsCountByThisWeek(String thisWeekMonday);

    /**
     * 查询本月应约人数
     *
     * @param firstDay4ThisMonth 本月日期
     * @return 应约人数
     */
    @Select("select count(1) from  t_order where orderDate>=#{firstDay4ThisMonth} and orderStatus ='已到诊' ")
    Integer findVisitsCountByThisMonth(String firstDay4ThisMonth);

    /**
     * 查询预约套餐最多的前四个
     *
     * @return 套餐名称  被预约次数  百分比
     *
     * 占比保留两位小数  select s.name,COUNT(o.id) setmeal_count, ROUND( COUNT(o.id)/ (select COUNT(0) from t_order) ,2)proportion from t_order o,t_setmeal s where s.id=o.setmeal_id group by s.name order by setmeal_count desc limit 0,4
     */
    @Select("select s.name,COUNT(o.id) setmeal_count,COUNT(o.id)/ (select COUNT(0) from t_order) proportion from t_order o,t_setmeal s where s.id=o.setmeal_id group by s.name order by setmeal_count desc limit 0,4 ")
    List<Map> findHotSetmeal();

}
